import { fetchComments } from '../../../services/comments/fetchComments';
import { fetchCommentsCount } from '../../../services/comments/fetchCommentsCount';
import dayjs from 'dayjs';
import {fetchCommodityComment} from "../../../utils/request";
const layoutMap = {
  0: 'vertical',
};
Page({
  data: {
    pageLoading: false,
    commentList: [],
    pageNum: 1,
    myPageNum: 1,
    pageSize: 10,
    total: 0,
    myTotal: 0,
    hasLoaded: false,
    layoutText: layoutMap[0],
    loadMoreStatus: 0,
    myLoadStatus: 0,
    spuId: '1060004',
    commentLevel: '',
    hasImage: '',
    commentType: '',
    totalCount: 0,
    countObj: {
      badCount: '0',
      commentCount: '0',
      goodCount: '0'
    },
  },
  async onLoad(options) {
    this.setData({
      spuId: options.spuId,
      countObj: {
        badCount: options.badCount,
        goodCount: options.goodCount,
        commentCount: options.total,
      }
    });
    await this.getComments(options);
  },
  generalQueryData(reset) {
    const { pageNum, pageSize, spuId, commentType} = this.data;
    const params = {
      pageNum: 1,
      pageSize: 20,
      goodComment: commentType,
      commodityId: spuId
    };
    // 重置请求
    if (reset) return params;

    return {
      ...params,
      pageNum: pageNum + 1,
      pageSize,
    };
  },
  async init(reset = true) {
    const { loadMoreStatus, commentList = [] } = this.data;
    const params = this.generalQueryData(reset);

    // 在加载中或者无更多数据，直接返回
    if (loadMoreStatus !== 0) return;

    this.setData({
      loadMoreStatus: 1,
    });

    const res = await fetchCommodityComment(params);
    console.log("fetchCommodityComment", res.data.content);
    const pageList = res.data.content;
    const _commentList = reset ? pageList : commentList.concat(pageList);
    let totalCount;
    if (params.goodComment == -1) {
      totalCount = this.data.countObj.commentCount;
    } else if (params.goodComment == 0) {
      totalCount = this.data.countObj.badCount;
    } else {
      totalCount = this.data.countObj.goodCount;
    }
    const _loadMoreStatus =
        _commentList.length === Number(totalCount) ? 2 : 0;
    this.setData({
      commentList: _commentList,
      pageNum: params.pageNum || 1,
      totalCount: Number(totalCount),
      loadMoreStatus: _loadMoreStatus,
    });
    this.setData({
      hasLoaded: true,
    });
  },
  getScoreArray(score) {
    var array = [];
    for (let i = 0; i < 5; i++) {
      if (i < score) {
        array.push(2);
      } else {
        array.push(0);
      }
    }
    return array;
  },
  async getComments() {
    await this.init(true);
  },
  async changeTag(e) {
    var { commenttype } = e.currentTarget.dataset;
    var { commentType } = this.data;
    if (commentType === commenttype) return;
    this.setData({
      loadMoreStatus: 0,
      commentList: [],
      total: 0,
      myTotal: 0,
      myPageNum: 1,
      pageNum: 1,
    });
    this.setData({
      myLoadStatus: 1,
      commentType: commenttype,
    });
    await this.init(true);
  },
  onReachBottom() {
    const { total = 0, commentList } = this.data;
    if (commentList.length === total) {
      this.setData({
        loadMoreStatus: 2,
      });
      return;
    }

    this.init(false);
  },
});
